package com._21cn.cron.runner;

import com._21cn.cms.article.entity.Article;
import com._21cn.cms.article.service.ArticleService;
import com._21cn.cms.template.utils.TemplateHelper;
import com._21cn.framework.flip.PageList;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("staticFileRunner")
public class StaticFileRunner
{

    @Autowired
    private TemplateHelper templateHelper;

    @Autowired
    private ArticleService articleService;
    private static String templatePath = "/data/qiaoliba/template/others/";
    private static String pagePath = "/data/qiaoliba/page/mama.isQiaoliba.com/xml/";

    public void generateXywyXML()
    {
        try
        {
            PageList list = getMamaList();
            Map maps = new HashMap();
            maps.put("articleList", list);
            File file = new File(templatePath + "index.xml");
            String templateContent = FileUtils.readFileToString(file, "utf8");
            String pageContent = TemplateHelper.evaluateTemplate(templateContent, new Map[] { maps });
            this.templateHelper.generateFile(pagePath + "index.xml", pageContent, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void generateXywyRSS()
    {
        try
        {
            PageList list = getMamaList();
            Map maps = new HashMap();
            maps.put("articleList", list);

            File file = new File(templatePath + "rss.xml");
            String templateContent = FileUtils.readFileToString(file, "utf8");
            String pageContent = TemplateHelper.evaluateTemplate(templateContent, new Map[] { maps });

            this.templateHelper.generateFile(pagePath + "rss.xml", pageContent, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private PageList<Article> getMamaList()
    {
        Map params = new HashMap();
        params.put("isPublished", Integer.valueOf(1));
        params.put("eqColumnId", Integer.valueOf(17));
        params.put("orderByClause", "PUBLISH_TIME desc");
        PageList<Article> list = this.articleService.getList(params, 1, 100, false);
        for (Article article : list) {
            Article art = this.articleService.getById(article.getId());
            article.setContent(art.getContent());
        }
        return list;
    }
}